/**
 * CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

package com.cmcc.zysoft.infocollection.mobile.controller;

import java.util.List;
import java.util.Map;
import java.util.HashMap;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import com.cmcc.zysoft.framework.utils.UUIDUtil;
import com.cmcc.zysoft.infocollection.mobile.service.MDeviceService;
import com.cmcc.zysoft.infocollection.mobile.service.MInterfaceLogService;
import com.cmcc.zysoft.infocollection.mobile.service.MSystemUserService;
import com.cmcc.zysoft.infocollection.model.Device;
import com.cmcc.zysoft.infocollection.model.SystemUser;
import com.cmcc.zysoft.infocollection.util.MD5Tools;
import com.cmcc.zysoft.infocollection.util.SendMsgTest;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cmcc.zysoft.infocollection.aop.LoginLogAspect;
import com.cmcc.zysoft.infocollection.common.BaseController;
import com.cmcc.zysoft.spring.security.util.SecurityContextUtil;

import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author 袁凤建
 * <br />邮箱：yuan.fengjian@ustcinfo.com
 * <br />描述：MLoginController.java
 * <br />版本: 1.0.0
 * <br />日期：2013-8-6 下午3:07:02
 * <br />CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

@Controller
@RequestMapping("/mobile")
public class MLoginController extends BaseController {

	/**
	 * 属性名称: mSystemUserService.
	 * 类       型: MSystemUserService.
	 */
	@Resource
	private MSystemUserService mSystemUserService;
	
	/**
	 * 属性名称: mDeviceService.
	 * 类       型: MDeviceService.
	 */
	@Resource
	private MDeviceService mDeviceService;
	
	/**
	 * 属性名称: mInterfaceLogService.
	 * 类       型: MInterfaceLogService.
	 */
	@Resource
	private MInterfaceLogService mInterfaceLogService;
	
	/**
	 * 登录接口-通过该接口,用户需要通过此接口将手机号和IMSI号、IMEI号绑定.
	 * @param usercode 手机号
	 * @param password 动态验证码
	 * @param IMEI 手机设备号
	 * @param IMSI SIM卡号
	 * @param version 版本号
	 * @return map
	 */
	@RequestMapping("/login.htm")
	@ResponseBody
	public Map<String, Object> login(HttpServletRequest request, String usercode, String password, String IMEI, String IMSI, String version) {
		LoginLogAspect.TIME_COMSUME_MAP.put(usercode, System.currentTimeMillis());
		Map<String, Object> map = this.mSystemUserService.mlogin(usercode, password, IMEI, IMSI, version);
		String IP = SecurityContextUtil.getRemoteIP(request);
		map.put("loginIp", IP);
		return map;
	}
	
	/**
	 * 手机侧注册.
	 * @param usercode 手机号
	 * @param type 操作类型 0-注册
	 * @param IMEI 手机设备号
	 * @param IMSI SIM卡号
	 * @return map
	 */
	@RequestMapping("/register.htm")
	@Transactional
	@ResponseBody
	public Map<String, Object> register(String usercode, String type, String IMEI, String IMSI) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<SystemUser> list = this.mSystemUserService.findByNamedParam(new String[]{"userName", "delFlag"}, new Object[]{usercode, "0"});
		if(!list.isEmpty()) {
			SystemUser systemUser = list.get(0);
			List<Device> list_dev = this.mDeviceService.findByNamedParam("employeeId", systemUser.getUserId());
			boolean isForbidden = false;
			if(!list_dev.isEmpty()) {
				if("0".equals(list_dev.get(0).getStatus())) { //设备状态: 1-可用 0-不可用
					isForbidden = true;
				}
			}
			if(isForbidden) {
				map.put("status", "2");
				map.put("message", "设备被禁用,禁止注册");
			} else {
				String salt = UUIDUtil.generateUUID();
				int number = (int)((Math.random() + 1) * 1000000);
				String code = "" + number;
				code = code.substring(1, 7);
				String password = MD5Tools.encodePassword(code, salt);
				String companyId = systemUser.getCompanyId();
				String content = "";
				if("0".equals(type)) {
					content = "尊敬的信息采集助手用户，您的密码是：" + code + "。输入密码即可登录。温馨提示：请勿将密码外泄！";
				} else {
					content = "尊敬的信息采集助手用户，您的新密码是: " + code + "。输入密码即可登录。温馨提示：请勿将密码外泄！";
				}
				String result = SendMsgTest.sendMsg(content, usercode);
				if("SUCCESS".equals(result)) {
					systemUser.setPassSalt(salt);
					systemUser.setPassword(password);
					this.mSystemUserService.updateEntity(systemUser);
					this.mDeviceService.addOrUpdateRegistercode(systemUser.getUserId(), usercode, IMEI, IMSI);
					if("0".equals(type)) {
						this.mInterfaceLogService.addLog(systemUser.getUserId(), companyId, "注册");
					} else {
						this.mInterfaceLogService.addLog(systemUser.getUserId(), companyId, "忘记密码");
					}
					map.put("status", "1");
					map.put("message", "注册成功");
				} else {
					map.put("status", "2");
					map.put("message", "注册失败");
				}
			}
		} else {
			map.put("status", "2");
			map.put("message", "用户不存在或已删除");
		}
		return map;
	}
	
	/**
	 * 三码校验接口-如果本地存在手机号,则在loading页对用户手机号、IMSI号、IMEI号做鉴权.
	 * @param usercode 手机号
	 * @param IMEI 手机设备号
	 * @param IMSI SIM卡号
	 * @return map
	 */
	@RequestMapping("/check.htm")
	@ResponseBody
	public Map<String, Object> check(String usercode, String IMEI, String IMSI) {
		return this.mSystemUserService.check(usercode, IMEI, IMSI);
	}
}